/**
 * Copyleft (C) 2023-2024 maminjie <canpool@163.com>
 * SPDX-License-Identifier: BSD-3-Clause
 **/
#ifndef QXMATERIALDRAWER_P_H
#define QXMATERIALDRAWER_P_H

#include "qxmaterial_global.h"
#include <QObject>
#include <QStateMachine>
#include "matoverlaywidget.h"

QX_MATERIAL_BEGIN_NAMESPACE

class MatDrawer;
class MatDrawerWidget;
class MatDrawerStateMachine;

class MatDrawerPrivate
{
    Q_DISABLE_COPY(MatDrawerPrivate)
    Q_DECLARE_PUBLIC(MatDrawer)
public:
    MatDrawerPrivate(MatDrawer *q);
    ~MatDrawerPrivate();

    void init();
    void setClosed(bool value = true);

    MatDrawer *const q_ptr;
    MatDrawerWidget *widget;
    MatDrawerStateMachine *stateMachine;
    QWidget *window;
    int width;
    bool clickToClose;
    bool autoRaise;
    bool closed;
    bool overlay;
};

class MatDrawerStateMachine : public QStateMachine
{
    Q_OBJECT

    Q_PROPERTY(qreal opacity WRITE setOpacity READ opacity)
public:
    explicit MatDrawerStateMachine(MatDrawerWidget *drawer, MatDrawer *parent);
    ~MatDrawerStateMachine();

    void setOpacity(qreal opacity);
    inline qreal opacity() const;

    bool isInClosedState() const;

    void updatePropertyAssignments();

signals:
    void signalOpen();
    void signalClose();
private:
    Q_DISABLE_COPY(MatDrawerStateMachine)

    MatDrawerWidget *const m_drawer;
    MatDrawer *const m_main;
    QState *const m_openingState;
    QState *const m_openedState;
    QState *const m_closingState;
    QState *const m_closedState;
    qreal m_opacity;
};

inline qreal MatDrawerStateMachine::opacity() const
{
    return m_opacity;
}

class MatDrawerWidget : public MatOverlayWidget
{
    Q_OBJECT

    Q_PROPERTY(int offset WRITE setOffset READ offset)
public:
    explicit MatDrawerWidget(QWidget *parent = 0);
    ~MatDrawerWidget();

    void setOffset(int offset);
    inline int offset() const;
protected:
    void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;

    QRect overlayGeometry() const Q_DECL_OVERRIDE;
private:
    int m_offset;
};

inline int MatDrawerWidget::offset() const
{
    return m_offset;
}

QX_MATERIAL_END_NAMESPACE

#endif   // QXMATERIALDRAWER_P_H
